<script setup>
import { RouterLink, RouterView } from 'vue-router'
import { ref, onMounted } from 'vue'

// 后端分类与前端标签映射（使用数据库 categories 表）
const categories = ref([])

// 当前选中的分类（保存为后端的 id）
const activeCategoryId = ref(null)

// 游戏数据（来自后端）
const games = ref([])
const filteredGames = ref([])

// 拉取分类
const fetchCategories = async () => {
  const resp = await fetch('/api/category/list/page', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ current: 1, pageSize: 50 })
  })
  const data = await resp.json()
  const list = data?.data?.records || []
  categories.value = list.map((c) => ({ id: c.id, name: c.name, icon: c.icon }))
  if (categories.value.length > 0) {
    activeCategoryId.value = categories.value[0].id
  }
}

// 拉取游戏（按分类过滤）
const fetchGames = async () => {
  const body = { current: 1, pageSize: 50 }
  if (activeCategoryId.value) {
    body.categoryId = activeCategoryId.value
  }
  const resp = await fetch('/api/game/list/page', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(body)
  })
  const data = await resp.json()
  const list = data?.data?.records || []
  games.value = list.map((g) => ({
    id: g.id,
    name: g.name,
    img: g.coverImageUrl || 'https://via.placeholder.com/150x100/777/FFFFFF?text=Game',
    score: '-',
    categoryId: g.categoryId
  }))
  filteredGames.value = games.value
}

const changeCategory = async (categoryId) => {
  activeCategoryId.value = categoryId
  await fetchGames()
  filteredGames.value = games.value.filter((g) => g.categoryId === categoryId)
}

onMounted(async () => {
  await fetchCategories()
  await fetchGames()
  if (activeCategoryId.value) {
    filteredGames.value = games.value.filter((g) => g.categoryId === activeCategoryId.value)
  }
})
</script>

<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="logo-container">
        <h1 class="logo">PlayFC.cn</h1>
        <div class="logo-subtitle">一起玩红白机</div>
      </div>
      
      <nav class="main-nav">
        <RouterLink to="/" class="nav-item">首页</RouterLink>
        <RouterLink to="/arcade" class="nav-item">街机</RouterLink>
        <RouterLink to="/casual" class="nav-item">休闲</RouterLink>
        <RouterLink to="/ranking" class="nav-item">排行榜</RouterLink>
        <RouterLink to="/arcade-ranking" class="nav-item">街机榜</RouterLink>
        <RouterLink to="/casual-ranking" class="nav-item">休闲榜</RouterLink>
        <RouterLink to="/room" class="nav-item">房间</RouterLink>
        <RouterLink to="/profile" class="nav-item">我的</RouterLink>
      </nav>
      
      <div class="user-actions">
        <RouterLink to="/login" class="btn-login">登录</RouterLink>
        <RouterLink to="/register" class="btn-register">注册</RouterLink>
      </div>
    </header>

    <!-- 主要内容区 -->
    <main class="main-content">
      <RouterView />
      
      <!-- 首页内容 -->
      <div class="home-content" v-if="$route.path === '/'">
        <!-- 游戏分类标签 -->
        <div class="category-tabs">
          <div 
            v-for="category in categories" 
            :key="category.id"
            :class="['category-tab', { active: activeCategoryId === category.id }]"
            @click="changeCategory(category.id)"
          >
            <span class="category-icon">{{ category.icon }}</span>
            <span class="category-name">{{ category.name }}</span>
          </div>
        </div>
        
        <!-- 游戏展示区 -->
        <div class="games-showcase">
          <h2 class="section-title">
            <span class="title-icon">🔥</span> 
            热门游戏
          </h2>
          
          <div class="games-grid">
            <div 
              v-for="game in filteredGames" 
              :key="game.id"
              class="game-card"
            >
              <div class="game-img">
                <img :src="game.img" :alt="game.name">
              </div>
              <div class="game-info">
                <h3 class="game-title">{{ game.name }}</h3>
                <div class="game-score">
                  <span class="score-icon">🏆</span>
                  <span class="score-value">{{ game.score }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
    
    <!-- 页脚 -->
    <footer class="footer">
      <p>© 2023 PlayFC.cn - 一起玩红白机 | 全网唯一可在线比拼分数的红白机网站</p>
    </footer>
  </div>
</template>

<style>
/* 全局样式 */
.app-container {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  width: 100%;
  margin: 0;
  padding: 0;
  color: #333;
}

/* 顶部导航栏 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-bottom: 2px solid #e63946;
  margin-bottom: 20px;
}

.logo-container {
  display: flex;
  flex-direction: column;
}

.logo {
  color: #e63946;
  font-size: 28px;
  font-weight: bold;
  margin: 0;
}

.logo-subtitle {
  font-size: 14px;
  color: #666;
}

.main-nav {
  display: flex;
  gap: 15px;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-weight: 500;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.nav-item:hover {
  background-color: #f8f9fa;
  color: #e63946;
}

.router-link-active {
  color: #e63946;
  border-bottom: 2px solid #e63946;
}

.user-actions {
  display: flex;
  gap: 10px;
}

.btn-login, .btn-register {
  padding: 8px 15px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s ease;
}

.btn-login {
  background-color: transparent;
  color: #e63946;
  border: 1px solid #e63946;
}

.btn-register {
  background-color: #e63946;
  color: white;
}

.btn-login:hover, .btn-register:hover {
  opacity: 0.9;
  transform: translateY(-2px);
}

/* 主要内容区 */
.main-content {
  min-height: calc(100vh - 200px);
}

/* 分类标签 */
.category-tabs {
  display: flex;
  gap: 15px;
  margin-bottom: 30px;
  border-bottom: 1px solid #eee;
  padding-bottom: 15px;
}

.category-tab {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: #f8f9fa;
}

.category-tab:hover {
  background-color: #e9ecef;
}

.category-tab.active {
  background-color: #e63946;
  color: white;
}

.category-icon {
  font-size: 20px;
}

/* 游戏展示区 */
.section-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 24px;
  margin-bottom: 20px;
  color: #333;
}

.title-icon {
  font-size: 24px;
}

.games-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 40px;
}

.game-card {
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  background-color: white;
}

.game-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
}

.game-img {
  height: 150px;
  overflow: hidden;
}

.game-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.game-card:hover .game-img img {
  transform: scale(1.05);
}

.game-info {
  padding: 15px;
}

.game-title {
  margin: 0 0 10px 0;
  font-size: 18px;
  color: #333;
}

.game-score {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #666;
}

.score-icon {
  color: #ffc107;
}

/* 页脚 */
.footer {
  text-align: center;
  padding: 20px 0;
  margin-top: 40px;
  border-top: 1px solid #eee;
  color: #666;
  font-size: 14px;
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;

    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>
